package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionTypes;
import cn.wolfcode.service.IExamQuestionService;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionItemService;
import cn.wolfcode.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/exam")
@Controller
public class ExamController {
    @Autowired
    private IExamService examService;
    @Autowired
    private IExamQuestionService examQuestionService;
    @Autowired
    private IQuestionService questionService;

    @RequestMapping("/input")
    public String input(){
        return "exam/input";
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Exam exam, QuestionTypes questionTypes){
        //把数据添加到试卷表中
        examService.save(exam);
        //把数据题目类型传到业务处理层处理,并添加到中间表
        examQuestionService.saveByQuestionTypes(questionTypes,exam.getId());
        JsonResult result = new JsonResult();
        result.setExamId(exam.getId());
        return result;
    }

    @RequestMapping("/show")
    public String show(Long id, Model model){
        //查询试卷的所有题目
        List<Question> questions =  questionService.listAllByExamId(id);
        //查询试卷信息
        Exam exam = examService.get(id);
        model.addAttribute("exam",exam);
        model.addAttribute("questions",questions);
        return "exam/show";
    }
}
